/**
 * 
 */
package cn.jy.erp.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.jy.erp.domain.SZResult;
import cn.jy.erp.domain.Sale;
import cn.jy.erp.domain.SalePurchase;
import cn.jy.erp.domain.SzName;
import cn.jy.erp.service.FnancialPlansService;


/**
 * @author 刘中斐
 *@date 2018年7月26日 上午10:22:27 
 */
@Controller
public class PlanController {

	@Autowired
	FnancialPlansService fnancialPlansService;
	/**
	 * 获取收入支出总额
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("map/getszjg")
	public @ResponseBody List<Object> getszjg(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		List<Object> list=fnancialPlansService.getdiff();
		if (callback != null && !"".equals(callback)) {

			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(list);

			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		return list;

	}

	/**
	 * 获取收入支出详细信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("map/getSZInformation")
	public @ResponseBody List<Object> getSZInformation(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String type=request.getParameter("type");
		String whereWeek=request.getParameter("whereWeek");
		if (callback != null && !"".equals(callback)) {

			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(fnancialPlansService.getBySZInformation(type,whereWeek));

			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		return null;

	}


	/**
	 * 获取采购销售总额
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("map/getTotal")
	public @ResponseBody List<Sale> getTotal(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		List<Sale> list= fnancialPlansService.getByTotal();
		if (callback != null && !"".equals(callback)) {

			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(list);

			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		return list;

	}

	/**
	 * 获取销售采购详细信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("map/getDetailed")
	public @ResponseBody Map<String, Object> getDetailed(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String whereWeek= request.getParameter("whereWeek");
		String rwlx=request.getParameter("rwlx");

		Map<String, Object> map= fnancialPlansService.getByDetailed(whereWeek,rwlx);
		if (callback != null && !"".equals(callback)) {

			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(map);

			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		return map;

	}

	@RequestMapping("map/getjy")
	public @ResponseBody List<Object> getjy(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		List<Object> list=fnancialPlansService.getjy();
		if (callback != null && !"".equals(callback)) {

			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(list);

			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		return list;

	}

	@RequestMapping("map/getSZDetails")
	public @ResponseBody Map<String,Object> getSZDetail(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		//List<Object> list=fnancialPlansService.getjy();
		Map<String, Object> map=fnancialPlansService.getSZDtail();
		if (callback != null && !"".equals(callback)) {

			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(map);

			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		return map;

	}

	@RequestMapping("map/updateXSTable")
	public void updateXSTable(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String SPMC=request.getParameter("SPMC");
		String SPXH=request.getParameter("SPXH");
		String SPJHSL=request.getParameter("SPJHSL");
		String SPJHDJ=request.getParameter("SPJHDJ");
		String SPJHZJ=request.getParameter("SPJHZJ");
		String SPID=request.getParameter("SPID");
		String SPJHSFSJ=request.getParameter("SPJHSFSJ");
		String PLANARRIVEDTIME=request.getParameter("PLANARRIVEDTIME");
		SalePurchase sa=new SalePurchase();
		sa.setSpmc(SPMC);
		sa.setSpxh(SPXH);
		if (SPJHSL!=null) {
			sa.setSpjhsl(Integer.valueOf(SPJHSL));
		}
		if (SPJHDJ!=null) {
			sa.setSpjhdj(Double.valueOf(SPJHDJ));
		}

		if (SPJHZJ!=null) {
			sa.setSpjhzj(Double.valueOf(SPJHZJ));
		}
		sa.setPlanarrivedtime(PLANARRIVEDTIME);
		SimpleDateFormat sfd=new SimpleDateFormat("yyyy-MM-dd");
		sa.setSpjhsfsj(sfd.parse(SPJHSFSJ));
		sa.setSpid(SPID);
		fnancialPlansService.updateXSTable(sa);
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString("修改成功");
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}


	}
	
	
	@RequestMapping("map/updateSZTable")
	public void updateSZTable(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String JHJE=request.getParameter("JHJE");
		String JHYF=request.getParameter("JHYF");
		String IECODE=request.getParameter("IECODE");
		SZResult sz=new SZResult();
		sz.setIECODE(IECODE);
		if (JHJE!=null) {
			sz.setJHJE(Double.valueOf(JHJE));
		}
		sz.setJHYF(JHYF);
		fnancialPlansService.updateSZTable(sz);
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString("修改成功");
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}
	
	@RequestMapping("map/getSZName")
	public @ResponseBody List<SzName> getSZName(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String SZLX=request.getParameter("SZLX");
		//List<Object> list=fnancialPlansService.getjy();
		List<SzName> szName = fnancialPlansService.getSzName(SZLX);
		if (callback != null && !"".equals(callback)) {

			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(szName);

			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		return szName;

	}
	
	
	
	@RequestMapping("map/deleteSp")
	public void deleteSp(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String spid=request.getParameter("spid");
		
		fnancialPlansService.deleteSP(spid);
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString("删除成功");
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}

		}
	}
	
	@RequestMapping("map/getSaleData")
	public @ResponseBody Map<String, Object> getSaleData(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String rwlx=request.getParameter("rwlx");
		String code=request.getParameter("code");
		
		Map<String, Object> saleData = fnancialPlansService.getSaleData(rwlx, code);
		if (callback != null && !"".equals(callback)) {

			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(saleData);

			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		return saleData;

	}
	
	
	
	@RequestMapping("map/getPayable")
	public @ResponseBody Map<String, Object> getPayable(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String type=request.getParameter("type");
		Map<String, Object> payable = fnancialPlansService.getPayable(type);
		if (callback != null && !"".equals(callback)) {

			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(payable);

			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		return payable;

	}
	
	
	@RequestMapping("map/getStackingChart")
	public @ResponseBody Map<String, Map<String, Object>> getStackingChart(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String type=request.getParameter("type");
		Map<String, Map<String, Object>> stackingChart = fnancialPlansService.getStackingChart(type);
		if (callback != null && !"".equals(callback)) {

			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(stackingChart);

			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		return stackingChart;

	}
	@RequestMapping("sr/getIncomeStacking")
	public @ResponseBody JSONObject getIncomeStacking(HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		String callback = request.getParameter("callback");
		String rwlx=request.getParameter("rwlx");
		JSONObject incomeStacking = fnancialPlansService.getIncomeStacking(rwlx);
		if (callback != null && !"".equals(callback)) {
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(incomeStacking);
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		return incomeStacking;

	}
}

